<?php

class AccessAction extends PublicAction {

    protected $db;

    function _initialize()
    {
        parent::_initialize();
        $this->db = M('Access');
    }

    function index()
    {
        $rid = intval($_GET['rid']);
        $alist = $this->db->where('role_id = '.$rid)->getField('node_id,role_id');
        $node = M('Node');

        $r = $node->where("pid=0 and status=1")->select();

        $this->assign('topnode', $r);

        $groups[0] = array('id'=>0,'name'=>L('ACCESS_PUBLIC'));
        foreach($this->menudata as $key=>$r){
            if($r['parentid']==0)
                $groups[$r[id]]=$r;
        }
        $this->assign('groups', $groups);


        foreach($groups as $key=>$res){
            $result=$node->where("groupid=$res[id] and status=1")->select();
            $array=array();
            foreach($result as $r) {
                $r['parentid']=$r['pid'];
                $r['selected'] = array_key_exists($r['id'],$alist)   ? 'checked' : '';
                $array[] = $r;
            }
            $nodes[$res['id']]['data']  =$array;
            $nodes[$res['id']]['groupinfo']=$res;
        }

        $node_app=$this->db->where("pid=0 and status=1")->select();
        $this->assign('node_app', $node_app);

        $this->assign('alist', $alist);
        $this->assign('node', $nodes);
        $this->assign('rid', $rid);
        $this->display();
    }

    function add()
    {
        $name = MODULE_NAME;
        $this->display('edit');
    }

    function insert()
    {
        $rid=$_POST['rid'];
        $nid=$_POST['nid'];

        if(!empty($rid)){
            if($nid){

                $node_id=implode(',',$nid);
                $node=M('Node');
                $list=$node->where('id in('.$node_id.')')->select();
                $this->db->where('role_id = '.$rid)->delete();
                foreach($list as $key=> $node){
                    $data[$key]['role_id']=$rid;
                    $data[$key]['node_id']=$node['id'];
                    $data[$key]["level"]=$node['level'];
                    $data[$key]["pid"]=$node['pid'];
                }

            $r=$this->db->addAll($data);

            }else{

                $r= $this->db->where('role_id = '.$rid)->delete();
            }
            if(false!==$r){
                $this->success(L('role_ok'));
            }else{

                $this->error(L('role_error'));
            }

        }else{
            $this->error(L('do_empty'));
        }
    }

    function update()
    {

        if($_POST['setup'])
            $_POST['setup'] = array2string($_POST['setup']);

        $name = MODULE_NAME;
        $model = D( $name );

        if (false === $model->create()) {
            $this->error($model->getError ());
        }

        if (false !== $model->save ()) {

            if(in_array(ucfirst($name),$this->cache_model))
                savecache($name);

            if($_POST['aid']){
                $Attachment =M('attachment');
                $aids =  implode(',',$_POST['aid']);
                $data['id'] = $_POST['id'];
                $data['catid'] = intval($_POST['catid']);
                $data['status'] = '1';
                $Attachment->where("aid in (".$aids.")")->save($data);
            }

            if($_POST['isajax'])
                $this->assign('dialog','1');

            $jumpUrl = $_POST['forward'] ? $_POST['forward'] : U(MODULE_NAME.'/index');

            $this->assign('jumpUrl', $jumpUrl);
            $this->success(L('edit_ok'));
        } else {
            $this->success (L('edit_error').': '.$model->getDbError());
        }
    }

}